package com.swotxu.screwweb.controller;

import cn.smallbun.screw.core.engine.EngineFileType;
import com.swotxu.screwweb.common.to.R;
import com.swotxu.screwweb.config.ScrewConfiguration;
import com.swotxu.screwweb.config.SecurityConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Set;

/**
 * @Date: 2021/7/13 18:15
 * @Author: xu.hai
 */
@Controller
public class IndexController {

    private static final Logger log = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private ScrewConfiguration configuration;
    @Autowired
    private SecurityConfig securityConfig;

    @GetMapping("/index")
    public String index() {
        return "forward:index.html";
    }

    @GetMapping("/getFormData")
    @ResponseBody
    public R getFormData() {
        Set<String> dbKeys = configuration.getScrewProperties().getIds();
        log.info("获取配置的数据库ids: {}", dbKeys);
        return R.ok().put("dbKeys", dbKeys).put("fileType", EngineFileType.values());
    }

    @GetMapping("/isOpenDownLoad")
    @ResponseBody
    public R isOpenDownLoad() {
        return R.ok().setData(securityConfig.isOpenDownLoad());
    }

    @RequestMapping("/setOpenDownLoad")
    @ResponseBody
    public R setOpenDownLoad(@RequestParam(name = "isOpen") boolean isOpen) {
        securityConfig.setOpenDownLoad(isOpen);
        return R.ok();
    }
}
